import { hasCoordinate, isTheSameCar } from '@/business/common'
import { ElMessage } from '@/stores/elMessage'
import type { CarInfo } from '@/types'
import { i18n } from '@/utils'
import { length, lineString } from '@turf/turf'
import { LineString, Marker, VectorLayer } from 'maptalks'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { clearStatus } from '.'
import { clearMenu, map } from './base'
import { clearDrawTool } from './drawTool'
import { clearPathLayer } from './path'
import { clearPathToolbarEvent } from './patrolPath'
import { templateDialogVisible } from './template'

export const layerId = 'record-point'
export const useRecordStore = defineStore('record', () => {
  const isRecord = ref<boolean>(false)
  const isRecordPath = ref(false)
  const recordPathLayer: VectorLayer = new VectorLayer(layerId)
  const recordMenu = [
    {
      item: i18n.global.t('jie-shu-lu-zhi'),
      click: endRecordPath
    }
  ]

  const recordPath = ref<[number, number][]>([])
  const filterRecordSum = computed(() => {
    return recordPath.value.length
  })
  let recordData: any = null
  const allRecordSum = ref(0)
  const recordPathPoints: Marker[] = []

  function initRecordPathLayer() {
    if (!map.getLayer(layerId)) {
      recordPathLayer.addTo(map)
    }
  }
  function endRecording() {
    if (isRecord.value) {
      ElMessage({
        type: 'error',
        message: i18n.global.t('qing-xian-jie-shu-lu-zhi')
      })
      return false
    } else {
      return true
    }
  }
  function recordPathToolbarEvent() {
    initRecordPathLayer()
    clearPathLayer()
    clearDrawTool()
    clearPathToolbarEvent()
    if (!isRecord.value) {
      recordPathPoints.length = 0
      recordPath.value.length = 0
      allRecordSum.value = 0
      isRecord.value = true
      ElMessage({
        type: 'success',
        message: i18n.global.t('kai-shi-lu-zhi')
      })
      setRecordMenu()
    } else if (isRecord.value) {
      ElMessage({
        type: 'warning',
        message: i18n.global.t('yi-kai-shi-lu-zhi-zhong')
      })
    }
  }
  function endRecordPath() {
    clearMenu()
    if (recordPathPoints.length > 1) {
      clearStatus()
      closeIsRecord()
      ElMessage({
        type: 'success',
        message: i18n.global.t('yi-jie-shu-lu-zhi-qing-bao-cun-lu-xian')
      })
      isRecordPath.value = true
      templateDialogVisible.value = true
    } else {
      ElMessage({
        type: 'warning',
        message: i18n.global.t('yi-jie-shu-lu-zhi-wei-cun-zai-lu-xian')
      })
      clearStatus()
      closeIsRecord()
      closeIsRecordPath()
    }
  }
  function setRecordMenu() {
    clearMenu()
    map.setMenu({
      width: 'auto',
      items: recordMenu
    })
  }

  function initRecordPath(data: CarInfo) {
    allRecordSum.value++

    // 筛选绘制保存录制路线
    if (recordData !== null) {
      const line = lineString([
        [recordData[0], recordData[1]],
        [data.longitude, data.latitude]
      ])
      const pointLength = length(line) * 1000
      //两点距离大于1米录制
      if (pointLength > 1) {
        drawRecordPath(data)
      }
    } else {
      drawRecordPath(data)
    }
    // 保存上一个值
    recordData = recordPath.value[recordPath.value.length - 1]
  }

  function drawRecordPath(data: CarInfo) {
    if (hasCoordinate(data) && isTheSameCar(data) && isRecord.value) {
      const pathPoint = new Marker([data.longitude as number, data.latitude as number])
      recordPath.value.push([Number(data.longitude), Number(data.latitude)])
      recordPathPoints.push(pathPoint)
      const connectLine = new LineString(recordPath.value, {
        symbol: {
          lineColor: 'red'
        }
      })
      recordPathLayer.addGeometry(connectLine)
    }
  }

  function closeIsRecordPath() {
    isRecordPath.value = false
    recordPathLayer.clear()
  }

  function closeIsRecord() {
    isRecord.value = false
    recordData = null
    recordPathLayer.clear()
  }

  const setIsRecord = (code: boolean) => {
    isRecord.value = code
  }

  return {
    endRecording,
    initRecordPathLayer,
    recordPathToolbarEvent,
    initRecordPath,
    filterRecordSum,
    allRecordSum,
    recordPathPoints,
    recordPathLayer,
    isRecordPath,
    closeIsRecordPath,
    isRecord,
    closeIsRecord,
    setIsRecord
  }
})
